package com.fjsaas.flyway.backup;

import java.io.BufferedReader;
import java.io.BufferedWriter; 
import java.io.FileInputStream; 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter; 
import java.nio.charset.Charset;

public class MysqlBackup {

	/** 访问MySQL数据库的用户名 */
	private String username;
	/** 访问MySQL数据库的密码 */
	private String password;
	/** 访问MySQL服务地址 */
	private String host;
	private String charset = "UTF-8";
	private String lineSeparator;
	private String dbName;
	private String dest;
	
	
	public MysqlBackup() {
		super();
		lineSeparator = System.getProperty("line.separator", "\n");
	}
	
	private String getBackupCommandStr(){
		StringBuilder sb = new StringBuilder();
		sb.append("cmd /E:ON /c mysqldump -u").append(username).append(" -p").
			append(password).append(" -h").append(host).
			append(" ").append("--default-character-set=utf8 ").append(dbName);
		return sb.toString();
	}
	
	private String getRestoreCommandStr(){
		StringBuilder sb = new StringBuilder();
		sb.append("cmd /E:ON /c mysql -u").append(username).append(" -p").
			append(password).append(" -h").append(host).
			append(" ").append(dbName);
		return sb.toString();
	}
	
	public void backup(){
		Process process = null;
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
			process = Runtime.getRuntime().exec(getBackupCommandStr());
			InputStream inputStream = process.getInputStream();
		    br = new BufferedReader(new InputStreamReader(inputStream,Charset.forName(charset)));  
	        String line = null;  
	        String outStr = null;
	        StringBuilder sb = new StringBuilder();  
			while ((line = br.readLine()) != null) {  
			     sb.append(line + lineSeparator);  
			}
			outStr = sb.toString();
			System.out.println(outStr);
			bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest)));
			bw.write(outStr);
			bw.flush();
		} catch (IOException e) {
			e.printStackTrace();			
		}finally{
			if(bw != null){
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}			
			
		}
	}

	public void backup(String dest, String dbName){
		this.dest = dest;
		this.dbName = dbName;
		backup();
						
	}
	
	public void restore(){
		Process process = null;
		BufferedWriter bw = null;
		BufferedReader br = null;
		try {
			process = Runtime.getRuntime().exec(getRestoreCommandStr());
			OutputStream out = process.getOutputStream();
			
			
			
			String line = null;
			String outStr = null;
			StringBuffer sb = new StringBuffer("");
			
		    br = new BufferedReader(new InputStreamReader(new FileInputStream(dest),
					Charset.forName(charset)));
			while ((line = br.readLine()) != null) {
				sb.append(line + lineSeparator);
			}
			outStr = sb.toString();			
			System.out.println(outStr);
		    
		    bw = new BufferedWriter(new OutputStreamWriter(out, Charset.forName(charset)));
			bw.write(outStr);
			bw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(br != null){
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}		
			
			if(bw != null){
				try {
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
						
		}		
	}
	
	public void restore(String dest, String dbName) {
		this.dest = dest;
		this.dbName = dbName;
		restore();
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public String getCharset() {
		return charset;
	}

	public void setCharset(String charset) {
		this.charset = charset;
	}

	public String getDbName() {
		return dbName;
	}

	public void setDbName(String dbName) {
		this.dbName = dbName;
	}

	public String getDest() {
		return dest;
	}

	public void setDest(String dest) {
		this.dest = dest;
	}
	
	
	
	
}
